package com.topscore.uke.mvp.presenters;

import com.topscore.uke.entities.AddressListItem;
import com.topscore.uke.mvp.contractors.SaveMemberAddressContractor;
import com.topscore.uke.mvp.model.SaveMemberAddressModel;
import com.topscore.uke.network.request.SaveMemberAddressRequest;

/**
 * Created by 71468 on 2017/11/21.
 */

public class SaveMemberAddressPresenter implements SaveMemberAddressContractor, SaveMemberAddressContractor.Presenter {
    private Model model;
    private View mView;

    private AddressListItem mMemberAddressItem;

    public SaveMemberAddressPresenter(View view) {
        this.mView = view;
        model = new SaveMemberAddressModel(new Model.CallBack() {
            @Override
            public void onSaveSuccess() {
                mView.onSaveAddressSuccess();
            }

            @Override
            public void onDeleteSuccess() {
                mView.onDeleteAddressSuccess(getMemberAddressItem());
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode,errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
    }

    @Override
    public void saveAddressInfo(SaveMemberAddressRequest request) {
        mView.onLoading("正在保存地址");
        model.save(request);
    }

    @Override
    public void deleteAddressItem(AddressListItem item) {
        setMemberAddressItem(item);
        mView.onLoading("正在删除");
        model.delete(item.getId());
    }

    private AddressListItem getMemberAddressItem() {
        return mMemberAddressItem;
    }

    private void setMemberAddressItem(AddressListItem memberAddressItem) {
        this.mMemberAddressItem = memberAddressItem;
    }
}
